<?php
// TODO: pom    工具类
namespace Admin\Common;

class Tools
{



    
    // TODO: 处理 页面 返回参数的公共函数
    static public function I($parms,$default)
    {
        $ret = null;
        if( isset( $_REQUEST[$parms] ) && !empty( $_REQUEST[$parms] ) ){
          $ret = $_REQUEST[$parms];
        }else{
          $ret = $default;
        }
        $ret = trim($ret);
        $ret = strip_tags($ret);
        return $ret;
    }


    // TODO: 程序内部网络操作时往往无法直接输出  使用这个函数输出到文件查看
    static public function fileDebug($data,$filename)
    {
      if( !isset($filename) || empty($filename ) ) $filename = "log.txt";
      ob_start();
      // var_dump($data);

      if( is_array($data) ){
        print_r($data);
      }else{
        echo $data;
      }

      $str = ob_get_clean();
      file_put_contents( dirname(__FILE__)."/".$filename, "\r\n----------------------------\r\n" , FILE_APPEND);
      file_put_contents( dirname(__FILE__)."/".$filename,  date("Y-m-d H:i:s")."\r\n" , FILE_APPEND);
      file_put_contents( dirname(__FILE__)."/".$filename, $str , FILE_APPEND);
    }



    // TODO: 防止sql注入，过滤危险字符
    static public function _search_lib_replace_end_tag($str)
    {
        if (empty($str)) return '';
        $str = htmlspecialchars($str);
        $str = stripslashes($str);

        $str = str_replace( '/', "", $str);
        $str = str_replace("\\", "", $str);
        $str = str_replace("&gt", "", $str);
        $str = str_replace("&lt", "", $str);
        $str = str_replace("<SCRIPT>", "", $str);
        $str = str_replace("</SCRIPT>", "", $str);
        $str = str_replace("<script>", "", $str);
        $str = str_replace("</script>", "", $str);
        $str=str_replace("select","",$str);
        $str=str_replace("join","",$str);
        $str=str_replace("union","",$str);
        $str=str_replace("where","",$str);
        $str=str_replace("insert","",$str);
        $str=str_replace("delete","",$str);
        $str=str_replace("update","",$str);
        $str=str_replace("like","like",$str);
        $str=str_replace("drop","",$str);
        $str=str_replace("create","",$str);
        $str=str_replace("modify","",$str);
        $str=str_replace("rename","",$str);
        $str=str_replace("alter","",$str);
        $str=str_replace("cas","cast",$str);
    	  $str=str_replace("&amp","&",$str);
        $str=str_replace("&","&",$str);
        $str=str_replace(">",">",$str);
        $str=str_replace("<","<",$str);
        /* $str=str_replace(" ",chr(32),$str);
        $str=str_replace(" ",chr(9),$str);
        $str=str_replace("    ",chr(9),$str);
        $str=str_replace("&",chr(34),$str);*/
        //$str=strstr($str,"'",true);
    	  $str=str_replace("'",chr(39),$str);
        $str=str_replace("'","\'",$str);
        $str=str_replace("<br />",chr(13),$str);
        $str=str_replace("''","",$str);
        $str=str_replace(";",'',$str);


        return $str;

    }








    static public function getData($url)
  	{
  		$ch = curl_init();
  		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  		curl_setopt($ch, CURLOPT_POST, 0);
  		curl_setopt($ch, CURLOPT_URL, $url);
  		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  		$ret  = curl_exec($ch);
  		$info = curl_getinfo($ch);
  		$err  = curl_errno($ch);
  		curl_close($ch);
  		if ( CURLE_OK != $err || 200 != $info["http_code"] ) {
  				if( self::$DEBUG ) {
            var_dump( $info ) ;exit;
          }
  				return null;
  		}
  		// var_dump($ret);exit;
  		return json_decode($ret,true);
  	}

  	static public function postData($url,$data)
  	{
  		$ch = curl_init();
  		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  		curl_setopt($ch, CURLOPT_POST, 1);
  		curl_setopt($ch, CURLOPT_URL, $url);
  		if( $data != null ){
  			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  		}
  		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  		$ret  = curl_exec($ch);
  		$info = curl_getinfo($ch);
  		$err  = curl_errno($ch);
  		curl_close($ch);
  		if ( CURLE_OK != $err || 200 != $info["http_code"] ) {
  			if( self::$DEBUG ) {
  				echo $info["http_code"];exit;
  			}
  			return null;
  		}
  		// var_dump($ret);exit;
  		return json_decode($ret,true);
  	}

    static public  $DEBUG  = false;


}






?>
